﻿using System;
using System.IO;
using ConsoleDraw;
using BLIT64;
using TBit.Compile;
using System.Collections.Generic;

namespace TBit
{
    class Program
    {
        public static VM vm;
        public static Game game;
        public static GUI.VMScene scene;

        static bool HasArg(string[] args,string str) {
            foreach(string s in args)
            {
                if(s == str)
                {
                    return true;
                }
            }
            return false;
        }

        static void Main(string[] args)
        {
            if(args.Length < 1) return;
            string code = "";

            vm = new VM(); 
            try {
                code = File.ReadAllText(args[0]);
            }
            catch(Exception) {
                return;
            }

            var asmc = new Compile.ASMCompile();
            var tokens = asmc.ProcessWord(code);

            vm.Init();
            asmc.Init();
            try
            {
                asmc.Compile(tokens,vm);
            }
            catch(ASMCompileError err)
            {
                System.Console.WriteLine($"Error : {err.Message}");
            }

            if(HasArg(args,"--gui"))
            {
                game = new Game("TBit",600,400);
                scene = new GUI.VMScene();
                scene.vm = vm;
                game.Run(scene);
            }
            else
            {
                vm.Run();
            }
        }
    }
}
